home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / editors / pman / sources / readpcx.c < prev    next >
C/C++ Source or Header  |  1993-03-21  |  16KB  |  345 lines

  1. /************************************************************************\
  2.  *                                                                      *
  3.  *  readpcx.c                                                           *
  4.  *                                                                      *
  5.  *  This file is part of PICTURE MAN image file converter/processor     *
  6.  *                                                                      *
  7.  *  1992 Potapov WORKS, STOIK Ltd.                                      *
  8.  *                                                                      *
  9.  *  This file contains source code for PCX file reader                  *
  10.  *                                                                      *
  11.  *  Compiler: MS C v.6.00A                                              *
  12.  *                                                                      *
  13.  *  You may use, modify and distribute this code freely                 *
  14.  *                                                                      *
  15. \************************************************************************/
  16. #include <windows.h>
  17. #include "custconv.h"
  18.   
  19. #define BUFSIZE 4096
  20.   
  21. #define SEEK_SET 0
  22. #define SEEK_END 2
  23.  
  24. typedef struct {
  25.                 int file, bplin, pos, buflen, count;
  26.                 BYTE byte, buffer[BUFSIZE];
  27.                 int iCur;
  28.                 }PCXStruct;
  29.   
  30. /************************************************************************\
  31.  *                                                                      *
  32.  * ROUTINE: int FAR PASCAL LibMain(HANDLE hModule, WORD wDataSeg,       *
  33.  *                                 WORD cbHeapSize, LPSTR lpszCmdLine)  *
  34.  * PURPOSE: DLL entry point.                                            *
  35.  *                                                                      *
  36. \************************************************************************/
  37. int FAR PASCAL LibMain(HANDLE hModule, WORD wDataSeg,
  38. WORD cbHeapSize, LPSTR lpszCmdLine)
  39. {
  40.     return 1;
  41. }
  42.   
  43. /************************************************************************\
  44.  *                                                                      *
  45.  * ROUTINE: int FAR PASCAL WEP(int bSystemExit)                         *
  46.  *                                                                      *
  47.  * PURPOSE: DLL exit procedure                                          *
  48.  *                                                                      *
  49. \************************************************************************/
  50. int FAR PASCAL WEP(int bSystemExit)
  51. {
  52.     return 1;
  53. }
  54.   
  55. /************************************************************************\
  56.  *                                                                      *
  57.  * ROUTINE: WORD FAR PASCAL Magic(void)                                 *
  58.  *                                                                      *
  59.  * PURPOSE: Identification routine                                      *
  60.  * RETURNS: 'Magic number' SRC_MAGIC                                    *
  61.  *                                                                      *
  62. \************************************************************************/
  63. WORD FAR PASCAL Magic(void)
  64. {
  65.     return SRC_MAGIC;
  66. }
  67.   
  68. /************************************************************************\
  69.  *                                                                      *
  70.  * ROUTINE: DWORD FAR PASCAL GetFlags(void)                             *
  71.  *                                                                      *
  72.  * PURPOSE: Sets flag for SetOptions Dialog box                         *
  73.  *                                                                      *
  74.  * PARAMETERS: None                                                     *
  75.  *                                                                      *
  76.  * RETURNS:                                                             *
  77.  *          INFO_HASOPTIONS if converter supports SetOptions dialog box *
  78.  *          0ul  otherwise                                              *
  79.  *                                                                      *
  80. \************************************************************************/
  81. DWORD FAR PASCAL GetFlags(void)
  82. {
  83.     return 0ul;
  84. }
  85.   
  86. /************************************************************************\
  87.  *                                                                      *
  88.  * ROUTINE: void FAR PASCAL GetDescription(LPSTR str)                   *
  89.  *                                                                      *
  90.  * PURPOSE: Sets format name                                            *
  91.  *                                                                      *
  92.  * PARAMETERS: LPSTR str - pointer for format name storage. The length  *
  93.  *             name must be less than 40!                               *
  94.  *                                                                      *
  95. \************************************************************************/
  96. void FAR PASCAL GetDescription(LPSTR str)
  97. {
  98.     lstrcpy(str,"ZSoft PaintBrush PCX");
  99. }
  100.   
  101. /************************************************************************\
  102.  *                                                                      *
  103.  * ROUTINE: void FAR PASCAL GetExtension(LPSTR str)                     *
  104.  *                                                                      *
  105.  * PURPOSE: Sets format file extension                                  *
  106.  *                                                                      *
  107.  * PARAMETERS: LPSTR str - pointer for format file extension.           *
  108.  *                                                                      *
  109. \************************************************************************/
  110. void FAR PASCAL GetExtension(LPSTR str)
  111. {
  112.     lstrcpy(str,"PCX");
  113. }
  114.  
  115. /************************************************************************\
  116.  *                                                                      *
  117.  * ROUTINE: int FAR PASCAL GetPrivateSize()                             *
  118.  *                                                                      *
  119.  * PURPOSE: Returns size of private memory block                        *
  120.  *                                                                      *
  121.  *                                                                      *
  122. \************************************************************************/
  123. int FAR PASCAL GetPrivateSize()
  124. {
  125.    return sizeof(PCXStruct);
  126. }
  127.   
  128. /************************************************************************/
  129. /************************************************************************/
  130. /************************************************************************/
  131. /* PCX File Header */
  132. typedef struct {
  133.     char         manuf;          /* Always =10 for Paintbrush   */
  134.     char         hard;           /* Version information         */
  135.     char         encod;          /* Run-length encoding (=1)    */
  136.     char         bitpx;          /* Bits per pixel              */
  137.     unsigned     x1;             /* Picture dimensions (incl)   */
  138.     unsigned     y1;
  139.     unsigned     x2;
  140.     unsigned     y2;
  141.     unsigned     hres;           /* Display horiz resolution    */
  142.     unsigned     vres;           /* Display vert  resolution    */
  143.     char         clrma[48];      /* Pallete                     */
  144.     char         vmode;          /* (ignored)                   */
  145.     char         nplanes;        /* Number of planes (ver 2.5=0)*/
  146.     unsigned     bplin;          /* Bytes per line              */
  147.     unsigned     palinfo;        /* Palette Info (1=col, 2=gray)*/
  148.     unsigned     shres;          /* Scanner resolution          */
  149.     unsigned     svres;          /*                             */
  150.     char         xtra[54];       /* Extra space (filler)        */
  151. } pcxheader;
  152.   
  153. /************************************************************************\
  154.  *                                                                      *
  155.  * ROUTINE: int FAR PASCAL ReadHeader(LPINFO p, void FAR * lpPrivate)   *
  156.  *                                                                      *
  157.  * PURPOSE: Reads image header. Fills INFO structure.                   *
  158.  *                                                                      *
  159.  * PARAMETERS: LPINFO p - pointer to INFO structure                     *
  160.  *             void far * lpPrivate - pointer to                        *
  161.  *             custom structure to store private data                   *
  162.  * RETURNS:                                                             *
  163.  * OK         - Success                                                 *
  164.  * BADFORMAT  - Bad signature                                           *
  165.  * UNSUPPORTED- Unsupported subformat                                   *
  166.  * BADFILE    - Error in file structure                                 *
  167.  * CANNOTOPEN - Cannot open src/dest                                    *
  168.  * INTERNAL   - Reserved                                                *
  169.  * BADDLL     - Error initializing DLL                                  *
  170.  * BADREQUEST - Unsupported type of conversion                          *
  171.  * BADTEMPFILE- Error creating/reading tempfile                         *
  172.  * NOMEMORY   - No enough global heap                                   *
  173.  * NODISK     - No enough disk space                                    *
  174.  * USERABORT  - Cancelled by user                                       *
  175.  * BADPARMS   - Conflicting parameters                                  *
  176.  *                                                                      *
  177. \************************************************************************/
  178. int FAR PASCAL ReadHeader(LPINFO p, void far * lpPrivate)
  179. {
  180.     pcxheader hdr;
  181.     int i,j;
  182.     char c;
  183.     HANDLE hTmp;
  184.     PCXStruct far *lpPCXStruct = lpPrivate;
  185.   
  186.     if( _llseek(p->file,0l,SEEK_SET) == -1 ) return BADFILE;
  187.     if( _lread(p->file,(LPSTR)&hdr,sizeof(hdr)) != sizeof(hdr))
  188.         return BADFILE;
  189.     if( hdr.manuf != 10 || hdr.encod != 1 ) return BADFORMAT;
  190.     if( (hdr.bitpx != 1 || (hdr.nplanes != 1 && hdr.nplanes != 4)) &&
  191.         (hdr.bitpx != 8 || (hdr.nplanes != 1 && hdr.nplanes != 3)))
  192.         return UNSUPPORTED;
  193.   
  194.     p->w        = hdr.x2-hdr.x1+1;
  195.     p->h        = hdr.y2-hdr.y1+1;
  196.     p->nplanes  = hdr.nplanes;
  197.     p->bplin    = hdr.bplin;
  198.     p->xres     = (DWORD)hdr.hres;
  199.     p->yres     = (DWORD)hdr.vres;
  200.     p->resunit  = RES_SCREEN;
  201.     p->flags    = INFO_COMPRESSED | INFO_FORWARD;
  202.   
  203.     if( hdr.bitpx < 8 ) p->flags |= INFO_PACKED;
  204.     if( hdr.nplanes > 1 ) p->flags |= INFO_SEPARATE;
  205.   
  206.     p->bps      = hdr.bitpx * hdr.nplanes;
  207.     p->numcolors= ( p->bps == 24 ) ? 0u : (1u << p->bps);
  208.   
  209.     if(p->bps == 8)
  210.     {
  211.         if(_llseek(p->file,-0x301l,SEEK_END) == -1
  212.             || _lread(p->file,&c,1) != 1 ) return BADFILE;
  213.         if(c == 0x0C )
  214.         {
  215.             for(i=0;i<256;i++)
  216.                 for(j = 0; j < 3; j++)
  217.                     if(_lread(p->file,&(p->pal[j][i]),1) != 1 )
  218.                         return BADFILE;
  219.             if(_llseek(p->file,sizeof(pcxheader),SEEK_SET) == -1)
  220.                 return BADFILE;
  221.             p->flags |= INFO_COLOR | INFO_PALETTE;
  222.         }
  223.     }
  224.     else if(p->bps == 4)
  225.     {
  226.         for( i = 0; i < 16; i++)
  227.             for(j = 0; j < 3; j++)
  228.                 p->pal[j][i] = hdr.clrma[i*3+j];
  229.         p->flags |= INFO_COLOR | INFO_PALETTE;
  230.     }
  231.     else if(p->bps == 24) p->flags |= INFO_COLOR;
  232.   
  233.     lpPCXStruct->file  = p->file;
  234.     lpPCXStruct->bplin = hdr.nplanes * hdr.bplin;
  235.     lpPCXStruct->count = 0;
  236.     lpPCXStruct->pos   = 0;
  237.     lpPCXStruct->buflen= 0;
  238.   
  239.     return OK;
  240. }
  241.   
  242. /************************************************************************/
  243. /************************************************************************/
  244. /************************************************************************/
  245. /* useful subfunction */
  246. static int getbuf(void far *lpPrivate)
  247. {
  248.  PCXStruct far *lpPCXStruct = lpPrivate;
  249.  
  250.     lpPCXStruct->pos = 1;
  251.   
  252.     if( (lpPCXStruct->buflen = _lread(lpPCXStruct->file,lpPCXStruct->buffer,BUFSIZE)) <= 0 ) return -1;
  253.     return (int)lpPCXStruct->buffer[0];
  254. }
  255.   
  256. /* useful subfunction */
  257. static int getbyte(void far *lpPrivate)
  258. {
  259.  PCXStruct far *lpPCXStruct = lpPrivate;
  260.  
  261.  
  262.  return (lpPCXStruct->pos<lpPCXStruct->buflen) ?
  263.  (int)lpPCXStruct->buffer[lpPCXStruct->pos++] :
  264.   getbuf(lpPrivate);
  265. }
  266.   
  267. /************************************************************************\
  268.  *                                                                      *
  269.  * ROUTINE: int FAR PASCAL ReadRow(int row, LPBYTE p,                   *
  270.  *                                         void far * lpPrivate)        *
  271.  *                                                                      *
  272.  * PURPOSE: Reads image row.                                            *
  273.  *                                                                      *
  274.  * PARAMETERS: int row - the number of row to read                      *
  275.  *             LPBYTE p - pointer to buffer to store image row          *
  276.  *             void far * lpPrivate - pointer to                        *
  277.  *             custom structure to store private data                   *
  278.  *                                                                      *
  279.  * RETURNS:                                                             *
  280.  * OK         - Success                                                 *
  281.  * BADFORMAT  - Bad signature                                           *
  282.  * UNSUPPORTED- Unsupported subformat                                   *
  283.  * BADFILE    - Error in file structure                                 *
  284.  * CANNOTOPEN - Cannot open src/dest                                    *
  285.  * INTERNAL   - Reserved                                                *
  286.  * BADDLL     - Error initializing DLL                                  *
  287.  * BADREQUEST - Unsupported type of conversion                          *
  288.  * BADTEMPFILE- Error creating/reading tempfile                         *
  289.  * NOMEMORY   - No enough global heap                                   *
  290.  * NODISK     - No enough disk space                                    *
  291.  * USERABORT  - Cancelled by user                                       *
  292.  * BADPARMS   - Conflicting parameters                                  *
  293.  *                                                                      *
  294. \************************************************************************/
  295. int FAR PASCAL ReadRow(int row, LPBYTE p, void far *lpPrivate)
  296. {
  297.     int len,n;
  298.     LPBYTE buf;
  299.     PCXStruct far *lpPCXStruct = lpPrivate;
  300.   
  301.     while(lpPCXStruct->iCur <= row)
  302.     {
  303.         buf = p;
  304.         len = lpPCXStruct->bplin;
  305.         while(len)
  306.         {
  307.             while(lpPCXStruct->count && len)
  308.             {
  309.                 *buf++ = lpPCXStruct->byte; lpPCXStruct->count--; len--;
  310.             }
  311.   
  312.             if(!len) goto Done;
  313.   
  314.             if( ( n = getbyte(lpPrivate)) == -1 ) return BADFILE;
  315.             if( ( n & 0xC0) == 0xC0 )
  316.             {
  317.                 lpPCXStruct->count = n & 0x3F;
  318.                 if( ( n = getbyte(lpPrivate)) == -1  ) return BADFILE;
  319.                 lpPCXStruct->byte = (BYTE)n;
  320.             }
  321.             else
  322.             {
  323.                 *buf++ = (BYTE)n; len--;
  324.             }
  325.         }
  326.   
  327.         Done:
  328.         lpPCXStruct->iCur++;
  329.     }
  330.     return OK;
  331. }
  332.   
  333. /************************************************************************\
  334.  *                                                                      *
  335.  * ROUTINE: void FAR PASCAL CleanUp(void far *lpPrivate)                *
  336.  *             void far * lpPrivate - pointer to                        *
  337.  *             custom structure to store private data                   *
  338.  *                                                                      *
  339.  * PURPOSE: Frees all auxiliary buffers. Terminates conversion.         *
  340.  *                                                                      *
  341. \************************************************************************/
  342. void FAR PASCAL CleanUp(void far *lpPrivate)
  343. {
  344. }
  345.